package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.shop.bean.Orders;
import com.shop.dao.IOrdersDao;
import com.shop.util.ConnDB;

/** 
 * @className: OrdersDaoImpl.java
 * @classDescription: 
 * @function: 
 * @author: Wentasy
 * @createTime: 2012-11-1 下午07:17:29
 * @modifyTime: 
 * @modifyReason: 
 * @since: JDK 1.6
 */
public class OrdersDaoImpl implements IOrdersDao{
	
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private String sql = "";
	private int affectedLine = 0;
	private Orders order = null;
	public int createOrder(Orders orders) throws Exception {
		Long userId = orders.getUserId();
		Timestamp orderDate = orders.getOrderDate();
		String payMode = orders.getPayMode();
		Boolean isPayed = orders.getIsPayed();
		Float totalPrice = orders.getTotalPrice();
		Long addressId = orders.getAddressId();
		sql = "insert into orders(userId, orderDate,  payMode, isPayed, totalPrice, addressId) values(?, ?, ?, ?, ?, ?)";
		
		conn = ConnDB.getConn();
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, userId);
		pstmt.setTimestamp(2, orderDate);
		pstmt.setString(3, payMode);
		pstmt.setBoolean(4, isPayed);
		pstmt.setFloat(5, totalPrice);
		pstmt.setLong(6, addressId);
		
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
	}

	public int deleteOrderById(long orderId) throws Exception {
		conn = ConnDB.getConn();
		sql = "delete from orders where orderId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderId);
		affectedLine = pstmt.executeUpdate();
	
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
	}

	public List<Orders> queryAllOrder() throws Exception {
		sql = "select * from orders";
		pstmt = conn.prepareStatement(sql);
		List<Orders> ordersList = new ArrayList<Orders>();
		rs = pstmt.executeQuery();
		Long orderId = 0L;
		Long userId = 0L;
		Timestamp orderDate = null;
		String payMode = "";
		Boolean isPayed = false;
		Float totalPrice = 0.0f;
		Long addressId = null;
		while(rs.next()){
			order = new Orders();
			orderId = rs.getLong("orderId");
			order.setOrderId(orderId);
			userId = rs.getLong("userId");
			order.setUserId(userId);
			orderDate = rs.getTimestamp("orderDate");
			order.setOrderDate(orderDate);
			payMode = rs.getString("payMode");
			order.setPayMode(payMode);
			isPayed = rs.getBoolean("isPayed");
			order.setIsPayed(isPayed);
			totalPrice = rs.getFloat("totalPrice");
			order.setTotalPrice(totalPrice);
			addressId = rs.getLong("addressId");
			order.setAddressId(addressId);
			ordersList.add(order);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return ordersList;
	}

	public List<Orders> queryAllOrder(int startPage, int pageSize)
			throws Exception {
		sql = "select * from orders limit(? - 1) * ?, ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, startPage);
		pstmt.setInt(2, pageSize);
		pstmt.setInt(3, pageSize);
		List<Orders> ordersList = new ArrayList<Orders>();
		rs = pstmt.executeQuery();
		Long orderId = 0L;
		Long userId = 0L;
		Timestamp orderDate = null;
		String payMode = "";
		Boolean isPayed = false;
		Float totalPrice = 0.0f;
		Long addressId = null;
		while(rs.next()){
			order = new Orders();
			orderId = rs.getLong("orderId");
			order.setOrderId(orderId);
			userId = rs.getLong("userId");
			order.setUserId(userId);
			orderDate = rs.getTimestamp("orderDate");
			order.setOrderDate(orderDate);
			payMode = rs.getString("payMode");
			order.setPayMode(payMode);
			isPayed = rs.getBoolean("isPayed");
			order.setIsPayed(isPayed);
			totalPrice = rs.getFloat("totalPrice");
			order.setTotalPrice(totalPrice);
			addressId = rs.getLong("addressId");
			order.setAddressId(addressId);
		}
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return ordersList;
	}

	public Orders queryOrderById(long orderId) throws Exception {
		sql = "select * from orders where orderId = ?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setLong(1, orderId);
		rs = pstmt.executeQuery();
		Long userId = null;
		Timestamp orderDate = null;
		String payMode = "";
		Boolean isPayed = false;
		Float totalPrice = 0.0f;
		Long addressId = null;
		while(rs.next()){
			userId = rs.getLong("userId");
			order.setUserId(userId);
			orderDate = rs.getTimestamp("orderDate");
			order.setOrderDate(orderDate);
			payMode = rs.getString("payMode");
			order.setPayMode(payMode);
			isPayed = rs.getBoolean("isPayed");
			order.setIsPayed(isPayed);
			totalPrice = rs.getFloat("totalPrice");
			order.setTotalPrice(totalPrice);
			addressId = rs.getLong("addressId");
			order.setAddressId(addressId);
		}
		
		ConnDB.close(rs);
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return order;
	}

	public int updateOrder(Orders orders) throws Exception {
		Long orderId = orders.getOrderId();
		Timestamp orderDate = orders.getOrderDate();
		String payMode = orders.getPayMode();
		Boolean isPayed = orders.getIsPayed();
		Float totalPrice = orders.getTotalPrice();
		Long addressId = orders.getAddressId();
		
		sql = "update orders set orderDate = ?, payMode = ?, isPayed = ?, totalPrice = ?, addressId = ? where orderId = ?";
		
		pstmt = conn.prepareStatement(sql);
		pstmt.setTimestamp(1, orderDate);
		pstmt.setString(2, payMode);
		pstmt.setBoolean(3, isPayed);
		pstmt.setFloat(4, totalPrice);
		pstmt.setLong(5, addressId);
		pstmt.setLong(5, orderId);
		
		affectedLine = pstmt.executeUpdate();
		ConnDB.close(pstmt);
		ConnDB.close(conn);
		return affectedLine;
	}

}
